<template>
  <div class="modify-dialog">
    <slot>
      <el-button size="small" type="primary" @click="showDialog()">
        新增人员
      </el-button>
    </slot>
    <el-dialog v-bind="dialogOpts" :visible.sync="dialogVisible">
      <Form v-bind="formOpts" v-on="formEvent">
        <template #formBtn>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="onConfirm"> 确 定 </el-button>
          </div>
        </template>
      </Form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ProductInput from "./ProductInput.vue";
import modifyMixins from "@/mixins/modifyMixins.vue";
export default {
  mixins: [modifyMixins],
  components: {
    ProductInput,
  },
  data() {
    return {
      dialogWidth: "600px",
      form: {
        roleId: {
          tag: "el-select",
          label: "用户角色",
          value: "",
          items: [],
          props: {
            clearable: true,
            filterable: true,
          },
        },
        userName: {
          tag: "el-input",
          label: "姓名",
          value: "",
          props: {
            clearable: true,
            placeholder: "请输入姓名",
          },
        },
        phoneNum: {
          tag: "el-input",
          label: "手机号",
          value: "",
          props: {
            clearable: true,
            placeholder: "请输入手机号",
          },
        },
        password: {
          tag: "el-input",
          label: "密码",
          value: "",
          props: {
            clearable: true,
            showPassword: true,
            placeholder: "请输入密码",
          },
        },
      },
    };
  },
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    showDialog(modify = "add", row = {}) {
      this.dialogVisible = true;
      this.row = row;
      this.modify = modify || "add";
      this.form.roleId.items = this.$store.getters.roleItems;
      for (const key in this.form) {
        let defValue = "";
        if (Array.isArray(this.form[key].value)) defValue = [];
        if (Object.hasOwnProperty.call(row, key))
          this.form[key].value = row[key];
        else this.form[key].value = defValue;
      }
      this.formKey = this.$quinn.guid();
    },
    onSubmit(form) {
      const { modify, row } = this;
      if (modify === "add")
        this.$api["userAddUser"](form).then(this.submitDoneMsg);
      else if (["edit"].includes(modify))
        this.$api["userUserInfoUpdate"]({
          ...form,
          userId: row.userId,
        }).then(this.submitDoneMsg);
    },
  },
};
</script>

<style lang="less" scoped></style>
